include('shared.lua')

CreateConVar( "cl_drawf302flame", "1" )
local matHeatWave		= Material( "sprites/heatwave" )
local matFire			= Material( "effects/fire_cloud1" )
local matPlasma			= Material( "effects/strider_muzzle" )

ENT.RenderGroup 		= RENDERGROUP_BOTH

function ENT:Initialize()
	self.ShouldDraw = 1
	self.NextSmokeEffect = 0
	
	// Make the render bounds a bigger so the effect doesn't get snipped off
	local mx, mn = self.Entity:GetRenderBounds()
	self.Entity:SetRenderBounds( mn + Vector(0,0,256), mx, 0 )
	
	self.Seed = math.Rand( 0, 10000 )

end

/*---------------------------------------------------------
   Name: Draw
---------------------------------------------------------*/
function ENT:Draw()

	self.BaseClass.Draw( self )
			
end

/*---------------------------------------------------------
   Name: DrawTranslucent
   Desc: Draw translucent
---------------------------------------------------------*/
function ENT:DrawTranslucent()

	if ( self.ShouldDraw == 0 ) then return end

	self.BaseClass.DrawTranslucent( self )
		
	if ( !self:IsOn() ) then 
		self.OnStart = nil
	return end
	
	if ( self:GetEffect() == "none" ) then return end
	
	self.OnStart = self.OnStart or CurTime()
	
	local EffectThink = self[ "EffectDraw_"..self:GetEffect() ]
	if ( EffectThink ) then EffectThink( self ) end
	
end


/*---------------------------------------------------------
   Name: Think
   Desc: Client Think - called every frame
---------------------------------------------------------*/
function ENT:Think()
	self.ShouldDraw = GetConVarNumber( "cl_drawf302flame" )
	
	local bDraw = true

	if ( self.ShouldDraw == 0 ) then bDraw = false end
	if ( !self:IsOn() ) then bDraw = false end
	if ( self:GetEffect() == "none" ) then bDraw = false end

	if ( !bDraw ) then return end
	local EffectThink = self[ "EffectThink_"..self:GetEffect() ]
	if ( EffectThink ) then EffectThink( self ) end

end

function ENT:EffectDraw_plasma()
	local vOffset = self.Entity:LocalToWorld( self:GetOffset())
	local vOffsetL = vOffset + self.Entity:GetUp()*-82.5 + self.Entity:GetForward()*-300 + self.Entity:GetRight()*5;   -- Specify Start Pos
	local vOffsetR = vOffset + self.Entity:GetUp()*-82.5 + self.Entity:GetForward()*-300 + self.Entity:GetRight()*208;   -- Specify Start Pos
	local vNormalL = vOffsetL - self.Entity:GetPos()
	local vNormalR = vOffsetR - self.Entity:GetPos()
	local vNormalL = (vNormalL + self.Entity:GetRight()*100 + self.Entity:GetUp()*-82.5):GetNormalized()   -- Specify Tail Pos
	local vNormalR = (vNormalR + self.Entity:GetRight()*-110 + self.Entity:GetUp()*-82.5):GetNormalized()  -- Specify Tail Pos

	local scroll = CurTime() * -20
		
	render.SetMaterial( matPlasma )
	
	scroll = scroll * 0.9
	if (self:GetMovingForward()) then
	--LEFT 'Engine'
	render.StartBeam( 3 )
		render.AddBeam( vOffsetL, 32, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffsetL + vNormalL * 16, 32, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffsetL + vNormalL * 128, 32, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffsetL, 32, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffsetL + vNormalL * 16, 32, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffsetL + vNormalL * 128, 32, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffsetL, 32, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffsetL + vNormalL * 16, 32, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffsetL + vNormalL * 128, 32, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	--Right 'Engine'
	render.StartBeam( 3 )
		render.AddBeam( vOffsetR, 32, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffsetR + vNormalR * 16, 32, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffsetR + vNormalR * 128, 32, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffsetR, 32, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffsetR + vNormalR * 16, 32, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffsetR + vNormalR * 128, 32, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffsetR, 32, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffsetR + vNormalR * 16, 32, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffsetR + vNormalR * 128, 32, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	end
	--AFTER BURNER!!!
	if (self:GetAfterBurner()) then
		local vOffsetC = vOffset + self.Entity:GetUp()*-70 + self.Entity:GetForward()*-300 + self.Entity:GetRight()*110; -- Specify Start Pos
		local vNormalC = vOffsetC - self.Entity:GetPos()
		local vNormalC = (vNormalC + self.Entity:GetRight() +self.Entity:GetUp()*-70):GetNormalized()   -- Specify Tail Pos
		
		render.StartBeam( 3 )
			render.AddBeam( vOffsetC, 96, scroll, Color( 0, 255, 255, 255) )
			render.AddBeam( vOffsetC + vNormalC * 16, 96, scroll + 0.01, Color( 255, 255, 255, 255) )
			render.AddBeam( vOffsetC + vNormalC * 128, 96, scroll + 0.02, Color( 0, 255, 255, 0) )
		render.EndBeam()
	
		scroll = scroll * 0.9
		
		render.StartBeam( 3 )
			render.AddBeam( vOffsetC, 96, scroll, Color( 0, 255, 255, 255) )
			render.AddBeam( vOffsetC + vNormalC * 16, 96, scroll + 0.01, Color( 255, 255, 255, 255) )
			render.AddBeam( vOffsetC + vNormalC * 128, 96, scroll + 0.02, Color( 0, 255, 255, 0) )
		render.EndBeam()
	
		scroll = scroll * 0.9
	
		render.StartBeam( 3 )
			render.AddBeam( vOffsetC, 96, scroll, Color( 0, 255, 255, 255) )
			render.AddBeam( vOffsetC + vNormalC * 16, 96, scroll + 0.01, Color( 255, 255, 255, 255) )
			render.AddBeam( vOffsetC + vNormalC * 128, 96, scroll + 0.02, Color( 0, 255, 255, 0) )
		render.EndBeam()
	end
end

/*----------------------------------------------------------
Below this line, has NOTHING to do with the effect rendering!!
----------------------------------------------------------*/
function ViewPoint( ply, origin, angles, fov )
local dev=LocalPlayer():GetNetworkedEntity("F302",LocalPlayer())
local dist= -600
local add=Vector(0,0,0)
	if dev:GetNetworkedBool("FirstPerson") then
		dist = 20
		add = Vector(0,0,-100)
	else
		dist = -600
		add = Vector(0,0,100)
	end
	if LocalPlayer():GetNetworkedBool("isDriveF302",false) and dev~=LocalPlayer() and dev:IsValid() then
		local view = {}
			view.origin = dev:GetPos()+add+ply:GetAimVector():GetNormal()*dist
			view.angles = angles
		return view
	end
end
hook.Add("CalcView", "F302View", ViewPoint)
